<template>
  <div class="cate-page">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-button type="primary" @click="showAddcate">添加商品</el-button>
      <tree-grid
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        index-text="#"
        show-index
        stripe
        border
      >
        <!-- 是否有效的插槽 -->
        <template slot="isko" slot-scope="{ row }">
          <i
            v-if="!row.cat_deleted"
            class="el-icon-success"
            style="color: green"
          ></i>
          <i v-else class="el-icon-error" style="color: #c00000"></i>
        </template>
        <!-- 排序的插槽 -->
        <template slot="order" slot-scope="{ row }">
          <!-- <tag> 1 </tag> -->
          <!-- <tag> 12</tag> -->
          <!-- <tag> 123</tag> -->
          <el-tag v-if="row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <!-- 操作列 -->
        <template slot="obtain" slot-scope="{ row }">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="editCate"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteCate(row)"
          >
            删除
          </el-button>
        </template>
      </tree-grid>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[1, 2, 4, 6, 10]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      @close="addCateClose"
      title="添加商品分类"
      :visible.sync="addCatedialogVisible"
      width="50%"
    >
      <el-form
        :rules="rules"
        ref="addForm"
        :model="addCateform"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateform.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- v-model 设置默认选中值 页面到数据 -->
          <!-- options 数据 -->
          <!-- props 配置对象 {label}指定渲染的数据字段 -->
          <!-- checkStrictly 是否可以选中父节点上的按钮 -->
          <el-cascader
            v-model="cascaderValue"
            :options="parentCate"
            :props="{
              expandTrigger: 'hover',
              label: 'cat_name',
              value: 'cat_id',
              checkStrictly: true,
            }"
            @change="cascaderHandleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取商品分类的数据参数
      queryData: {
        pagenum: 1,
        pagesize: 10
      },
      cateList: [],
      total: 0,
      // 树形表格每一列的配置
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template', // 代表此类对应插槽
          template: 'isko'
        },
        {
          label: '排序',
          type: 'template', // 代表此类对应插槽
          template: 'order'
        },
        {
          label: '操作',
          type: 'template', // 代表此类对应插槽
          template: 'obtain'
        }
      ],
      addCatedialogVisible: false,
      // 添加表单数据
      addCateform: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 校验规则
      rules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      // 父级分类
      parentCate: [],
      cascaderValue: []
    }
  },

  created() {
    this.getGoodscateList()
  },

  methods: {
    // 页面整个数据请求
    async getGoodscateList() {
      const res = await this.$axios.get('categories', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 每页条数发送变化
    handleSizeChange(val) {
      this.queryData.pagesize = val
      // console.log(val)
      this.getGoodscateList()
    },
    // 页码发送变化
    handleCurrentChange(val) {
      // console.log(val)
      this.queryData.pagenum = val
      this.getGoodscateList()
    },
    // 点击添加分类
    async showAddcate() {
      this.addCatedialogVisible = true
      const res = await this.$axios.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res)
      this.parentCate = res.data
      // console.log(this.parentCate)
    },
    // 级联选择器发生变化
    cascaderHandleChange() {
      this.addCateform.cat_level = this.cascaderValue.length
      this.addCateform.cat_pid = this.cascaderValue[
        this.cascaderValue.length - 1
      ]
      console.log(this.addCateform.cat_level)
      console.log(this.addCateform.cat_pid)
    },
    // 点击确定
    addCate() {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        const res = await this.$axios.post('categories', this.addCateform)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.getGoodscateList()
        this.addCatedialogVisible = false
      })
    },
    addCateClose() {
      this.$refs.addForm.resetFields()
      this.cascaderValue = []
    },
    // 编辑数据
    editCate() {
      console.log('编辑')
    },
    deleteCate(row) {
      // console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await this.$axios.delete('categories/' + row.cat_id)
          // console.log(res)
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.$message.success(res.meta.msg)
          this.getGoodscateList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang='less'>
.el-button {
  margin-bottom: 15px;
}
</style>
